package com.softwinner.bt.Fragment;

import com.softwinner.bt.CallDialog;
import com.softwinner.bt.MainActivity;
import com.softwinner.bt.R;
import com.softwinner.bt.policy.BTManager;
import com.softwinner.bt.util.Command;

import android.app.Fragment;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class KeyBoardFragment extends Fragment implements OnClickListener {
	
	private MainActivity mActivity;
	private ImageView mBack;
	private TextView mEdit;
	private Button mBtnNum0;
	private Button mBtnNum1;
	private Button mBtnNum2;
	private Button mBtnNum3;
	private Button mBtnNum4;
	private Button mBtnNum5;
	private Button mBtnNum6;
	private Button mBtnNum7;
	private Button mBtnNum8;
	private Button mBtnNum9;
	private Button mBtnNumAsterise;
	private Button mBtnNumPound;	
	private ImageView mDial;
	private ImageView mContact;
	private BTManager mBtManager;
	private StringBuffer mInput = new StringBuffer();
		
	@Override
	public View onCreateView(LayoutInflater inflater,
			 ViewGroup container,  Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.bt_keyboard_fragment, container, false);
		setupView(view);
		return view;
	}
	
	@Override
	public void onActivityCreated( Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mActivity = (MainActivity) getActivity();
		mBtManager = BTManager.getInstance();
	}
	
	public void setupView(View view){
		mBack = (ImageView)view.findViewById(R.id.keyboard_back);
		mEdit = (TextView)view.findViewById(R.id.keyboard_display);
		mBtnNum0 = (Button)view.findViewById(R.id.keyboard_num0);
		mBtnNum1 = (Button)view.findViewById(R.id.keyboard_num1);
		mBtnNum2 = (Button)view.findViewById(R.id.keyboard_num2);
		mBtnNum3 = (Button)view.findViewById(R.id.keyboard_num3);
		mBtnNum4 = (Button)view.findViewById(R.id.keyboard_num4);
		mBtnNum5 = (Button)view.findViewById(R.id.keyboard_num5);
		mBtnNum6 = (Button)view.findViewById(R.id.keyboard_num6);
		mBtnNum7 = (Button)view.findViewById(R.id.keyboard_num7);
		mBtnNum8 = (Button)view.findViewById(R.id.keyboard_num8);
		mBtnNum9 = (Button)view.findViewById(R.id.keyboard_num9);
		mBtnNumAsterise = (Button)view.findViewById(R.id.keyboard_num_asterisk);
		mBtnNumPound= (Button)view.findViewById(R.id.keyboard_num_pound);
		mDial = (ImageView)view.findViewById(R.id.keyboard_dial);
		mContact = (ImageView)view.findViewById(R.id.keyboard_contact);
		
		mBack.setOnClickListener(this);
		mEdit.setOnClickListener(this);
		mBtnNum0.setOnClickListener(this);
		mBtnNum1.setOnClickListener(this);
		mBtnNum2.setOnClickListener(this);
		mBtnNum3.setOnClickListener(this);
		mBtnNum4.setOnClickListener(this);
		mBtnNum5.setOnClickListener(this);
		mBtnNum6.setOnClickListener(this);
		mBtnNum7.setOnClickListener(this);
		mBtnNum8.setOnClickListener(this);
		mBtnNum9.setOnClickListener(this);
		mBtnNumAsterise.setOnClickListener(this);
		mBtnNumPound.setOnClickListener(this);
		mDial.setOnClickListener(this);
		mContact.setOnClickListener(this);
		mBack.setOnLongClickListener(new OnLongClickListener() {			
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				mInput = new StringBuffer();
				mEdit.setText(mInput);
				return false;
			}
		});
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(mEdit != null){
			mInput = new StringBuffer();
			mEdit.setText(mInput);
		}
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
        case R.id.keyboard_num0:
        	mInput.append("0");
        	break;
        case R.id.keyboard_num1:
        	mInput.append("1");
        	break;
        case R.id.keyboard_num2:
        	mInput.append("2");
        	break;
        case R.id.keyboard_num3:
        	mInput.append("3");
        	break;
        case R.id.keyboard_num4:
        	mInput.append("4");
        	break;
        case R.id.keyboard_num5:
        	mInput.append("5");
        	break;
        case R.id.keyboard_num6:
        	mInput.append("6");
        	break;
        case R.id.keyboard_num7:
        	mInput.append("7");
        	break;
        case R.id.keyboard_num8:
        	mInput.append("8");
        	break;
        case R.id.keyboard_num9:
        	mInput.append("9");
        	break;
        case R.id.keyboard_num_asterisk:
        	mInput.append("*");
        	break;
        case R.id.keyboard_num_pound:
        	mInput.append("#");
        	break;
        case R.id.keyboard_back:
            if (mInput.length() > 0) {
                mInput.deleteCharAt(mInput.length() - 1);
            }
            break;
        case R.id.keyboard_dial:
            if (mInput.length() > 0) {
            	if(mActivity.getState() != BTManager.STATE_CONNECTED){
            		Toast.makeText(mActivity, R.string.msg_unconnectedorcalling, 1000).show();
            		return;
            	}
            	mBtManager.sendCommand(Command.SEND_MAKECALL + mInput.toString());
            }
            break;
        case R.id.keyboard_contact:
            mActivity.SetFragment(MainActivity.TYPE_CONTACT);
            break;
        default:
            break;
        }
		mEdit.setText(mInput.toString());
    }
}
